﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace MotionControl.Modules.RestService.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WSController : ControllerBase
    {
        private readonly WSHandler _wsHandler;

        private readonly ILogger<WSController> _logger;
        public WSController(WSHandler wsHandler, ILogger<WSController> logger)
        {
            _wsHandler = wsHandler;
            _logger = logger;
        }

        [HttpGet]
        public async Task GetInfo()
        {
            if(HttpContext.WebSockets.IsWebSocketRequest)
            {
                var ws = await HttpContext.WebSockets.AcceptWebSocketAsync();
                Debug.WriteLine(ws);
                //var request = HttpContext.Request;
                //var baseUrl = $"{request.Scheme}://{request.Host}";
                await _wsHandler.HandleConnection(ws);
            }
            else
                HttpContext.Response.StatusCode = 400;
        }
    }
}
